/**
 * This file is part of Todo.txt for Android, an app for managing your todo.txt file (http://todotxt.com).
 * <p>
 * Copyright (c) 2009-2013 Todo.txt for Android contributors (http://todotxt.com)
 * <p>
 * LICENSE:
 * <p>
 * Todo.txt for Android is free software: you can redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
 * later version.
 * <p>
 * Todo.txt for Android is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * <p>
 * You should have received a copy of the GNU General Public License along with Todo.txt for Android. If not, see
 * <http://www.gnu.org/licenses/>.
 * <p>
 * Todo.txt for Android's source code is available at https://github.com/ginatrapani/todo.txt-android
 *
 * @author Todo.txt for Android contributors <todotxt@yahoogroups.com>
 * @license http://www.gnu.org/licenses/gpl.html
 * @copyright 2009-2013 Todo.txt for Android contributors (http://todotxt.com)
 */
package com.todotxt.todotxttouch;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class PeriodicSyncStarter extends BroadcastReceiver {
    public static void setupPeriodicSyncer(Context context) {
        TodoApplication a = (TodoApplication) context.getApplicationContext();
        AlarmManager alarms = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, SyncerService.class);
        PendingIntent pi = PendingIntent.getService(context, 0, i,
                PendingIntent.FLAG_UPDATE_CURRENT);
        alarms.cancel(pi); // Cancel any previously started
        long syncPeriod = a.m_prefs.getSyncPeriod();

        if (syncPeriod > 0) {
            // Wake up and synchronize after after inexact fixed delay
            alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
                    syncPeriod, syncPeriod, pi);
            // alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, 60 * 1000,
            // pi); // for testing
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            setupPeriodicSyncer(context);
        }
    }
}
